﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.ComponentModel;

namespace PromoServer.Library.Entities
{
    public class LayoutContent : INotifyPropertyChanged
    {
        // Declare the property changed event 
        public event PropertyChangedEventHandler PropertyChanged;

        public int ID { get; set; }
        public int LayoutID { get; set; }

        public string Name { get; set; }

        public int ScheduleID { get; set; }

        private Int32 _sequence;
        public Int32 Sequence 
        {
            get
            {
                return _sequence;
            }
            set
            {
                _sequence = value;
                // Call OnPropertyChanged whenever the property is updated
                OnPropertyChanged("LayoutDescription");
            }
        }

        public string DisplayDuration
        {
            get
            {
                string textDescription;
                if (_contentType.ToLower() == "image")
                    textDescription = "Forever";
                else if (_contentType.ToLower() == "video")
                    textDescription = "To end";
                else
                    textDescription = "0";

                if (_duration == 0)
                    return textDescription;
                else
                    return _duration.ToString();
            }
            set
            {
                string textDescription;
                if (_contentType.ToLower() == "image")
                    textDescription = "Forever";
                else if (_contentType.ToLower() == "video")
                    textDescription = "To end";
                else
                    textDescription = "0";

                if (value == textDescription)
                    _duration = 0;
                else
                    _duration = Convert.ToInt32(value);

                OnPropertyChanged("LayoutDescription");
            }
        }

        private Int32 _duration;
        public Int32 Duration
        {
            get
            {
                return _duration;
            }
            set
            {
                _duration = value;
                // Call OnPropertyChanged whenever the property is updated
                OnPropertyChanged("LayoutDescription");
            }
        }

        public string LayoutDescription
        {
            get
            {
                string layoutDescription = string.Empty;

                // Set sequence description
                string strSequence = Convert.ToString(Sequence);
                string numberSuffix = string.Empty;
                layoutDescription += "[Show " + strSequence;
                // Append sequence # suffix
                switch (strSequence.Substring(strSequence.Length - 1, 1))
                {
                    case "1":
                        numberSuffix = "st";
                        break;
                    case "2":
                        numberSuffix = "nd";
                        break;
                    case "3":
                        numberSuffix = "rd";
                        break;
                    default:
                        numberSuffix = "th";
                        break;
                }
                layoutDescription += numberSuffix;

                // Set duration description
                string strDuration = Convert.ToString(Duration);
                if (strDuration == "0")
                    layoutDescription += " forever]";
                else
                    layoutDescription += " for " + strDuration + " seconds]";

                // Return description
                return layoutDescription;
            }
        }

        private string _orientation;
        public string Orientation
        {
            get
            {
                // Set to upper case for display purposes
                return _orientation.ToUpper();
            }
            set
            {
                _orientation = value;
            }
        }

        public string _contentType;
        public string ContentType
        {
            get
            {
                return _contentType;
            }
            set
            {
                _contentType = value;
            }
        }

        public string ContentFileName { get; set; }
        public string ContentFileExtension 
        {
            get
            {
                if (ContentFileName != string.Empty)
                {
                    FileInfo fi = new FileInfo(ContentFileName);
                    return fi.Extension;
                }
                else
                    return string.Empty;
            }
        }

        public bool Stretch { get; set; }

        private List<string> _supportedImageExtensions = new List<string>()
        {
            ".bmp",
            ".jpg",
            ".jpeg",
            ".png",
            ".gif",
            ".tif",
            ".tiff"
        };

        private List<string> _supportedVideoExtensions = new List<string>()
        {
            ".wmv",
            ".mpg",
            ".mpeg",
            ".avi"
        };

        public LayoutContent(Int32 sequence, Int32 duration, bool stretch, string contentType)
        {
            // Assign properties to empty layout
            ID = 0;
            _sequence = sequence;
            _duration = duration;
            _orientation = "L";
            Stretch = stretch;

            _contentType = contentType;
        }

        public LayoutContent(int layoutID, Int32 sequence, Int32 duration, bool stretch, string contentType)
        {
            // Assign properties to empty layout
            ID = 0;
            LayoutID = layoutID;
            _sequence = sequence;
            _duration = duration;
            _orientation = "L";
            Stretch = stretch;

            _contentType = contentType;
        }

        public LayoutContent(int id, int layoutID, Int32 sequence, Int32 duration, string orientation, string contentFileName, bool stretch)
        {
            // Assign properties to existing layout
            ID = id;
            LayoutID = layoutID;
            _sequence = sequence;
            _duration = duration;
            _orientation = orientation;
            ContentFileName = contentFileName;
            Stretch = stretch;

            // Set content type
            foreach (string imgExt in _supportedImageExtensions)
            {
                if (ContentFileExtension == imgExt)
                    _contentType = "image";
            }

            foreach (string vidExt in _supportedVideoExtensions)
            {
                if (ContentFileExtension == vidExt)
                    _contentType = "video";
            }
        }

        // Create the OnPropertyChanged method to raise the event 
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}
